package com.itboye.ihomebank.activity.key;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.ihomebank.KeyFragment;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.hetong.ActivityEditHeTongFangdong;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.ttKey.BleConstant;
import com.itboye.ihomebank.ttKey.Key;
import com.itboye.ihomebank.ttKey.Operation;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.YuYueChooseTimePopwindow;
import com.ttlock.bl.sdk.scanner.ExtendedBluetoothDevice;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Observable;

import static com.itboye.ihomebank.KeyFragment.mTTLockAPI;

/**
 * Created by Administrator on 2018/8/7 0007.
 */

public class ActivityAddICCard extends BaseOtherActivity implements YuYueChooseTimePopwindow.onFinishedLisenter {
    //title
    View v_statusbar;
    ImageView close_icon;
    TextView add_shap_title_tv;

    private TextView addDevice;
    private LinearLayout selectTimeLl;
    private RelativeLayout startTimeRl,endTimeRl;
    private TextView startTimeTv,endTimeTv;
    private ToggleButton toggle_button;
    private EditText nameEt;

    private UserPresenter userPresenter;
    private String textStringStart = "";
    private String textStringEnd = "";
    private  ExtendedBluetoothDevice device;

    public static String startDate;
    public static String endDate;
    public static Key localKey;
    public static String icCardName;

//    private int hour;
//    private int min;

    @Override
    protected int layout_id() {
        return R.layout.activity_addiccard;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("添加IC卡");
        //请求打开蓝牙
        if (ActivityGuanLiKey.activity.equals("one")){
            KeyFragment.mTTLockAPI.requestBleEnable(this);
        }else {
            ActivityGuanLiKey.mTTLockAPI.requestBleEnable(this);
        }

        userPresenter=new UserPresenter(this);

        localKey= ActivityGuanLiKey.localKey;

        toggle_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    selectTimeLl.setVisibility(View.GONE);
                }else {
                    selectTimeLl.setVisibility(View.VISIBLE);
                }
            }
        });
        this.registerReceiver(mReceiver,getIntentFilter());


    }

    @Override
    public void finish() {
        super.finish();
        this.unregisterReceiver(mReceiver);
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }
    private IntentFilter getIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BleConstant.ACTION_BLE_DEVICE);
        intentFilter.addAction(BleConstant.ACTION_BLE_DISCONNECTED);
        intentFilter.addAction(BleConstant.ADD_ICCARD);
        return intentFilter;
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (action.equals(BleConstant.ACTION_BLE_DEVICE)) {
                Bundle bundle = intent.getExtras();
                device = bundle.getParcelable(BleConstant.DEVICE);
                Log.d("activityadd====","shoudaole");
//                foundDeviceAdapter.updateDevice(device);
            } else if(action.equals(BleConstant.ACTION_BLE_DISCONNECTED)) {
//                closeProgressDialog();
//                finish();
//                Toast.makeText(ActivitySheBei.this, "蓝牙已断开,请重新添加.", Toast.LENGTH_LONG).show();
            }else if (action.equals(BleConstant.ADD_ICCARD)){
                closeProgressDialog();
                finish();
            }
        }
    };

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.close_icon:
                this.finish();
                break;

            case R.id.addDevice:
                if (nameEt.getText().toString().trim().equals("")){
                    ByAlert.alert("IC卡名字不能为空");
                    return;
                }
                if (toggle_button.isChecked()){
                    startDate="";
                    Log.d("=======",startDate+"");
                    endDate="";
                }else {
                    if (startTimeTv.getText().toString().equals("")||endTimeTv.getText().toString().equals("")){
                        ByAlert.alert("请选择生效时间和到期时间");
                        return;
                    }
                }
                icCardName=nameEt.getText().toString().trim();
                if (ActivityGuanLiKey.activity.equals("one")){
                    KeyFragment.bleSession.setOperation(Operation.ADDICCARD);
                    KeyFragment.mTTLockAPI.connect(localKey.getLockMac());
//                    KeyFragment.mTTLockAPI.connect(device);
                }else {//纯门锁
                    ActivityGuanLiKey.bleSession.setOperation(Operation.ADDICCARD);
                    ActivityGuanLiKey.mTTLockAPI.connect(localKey.getLockMac());
                }

                showProgressDialog("正在添加ic卡...",true);

                break;

            case R.id.startTimeRl:
                YuYueChooseTimePopwindow endTimePopwindow = new YuYueChooseTimePopwindow(
                        this, this, true);
                endTimePopwindow.getContentView().findViewById(R.id.time).setVisibility(View.VISIBLE);
                endTimePopwindow.getContentView().findViewById(R.id.min).setVisibility(View.VISIBLE);
                endTimePopwindow.showAtLocation(v, Gravity.BOTTOM
                        | Gravity.CENTER_HORIZONTAL, 0, 0);
                break;
            case R.id.endTimeRl:
                if (textStringStart.equals("")) {
                    ByAlert.alert("请先选择开始时间");
                    return;
                }
                YuYueChooseTimePopwindow endTimePopwindow1 = new YuYueChooseTimePopwindow(
                        this, this, false);
                endTimePopwindow1.getContentView().findViewById(R.id.time).setVisibility(View.VISIBLE);
                endTimePopwindow1.getContentView().findViewById(R.id.min).setVisibility(View.VISIBLE);
                endTimePopwindow1.showAtLocation(v, Gravity.BOTTOM
                        | Gravity.CENTER_HORIZONTAL, 0, 0);
                break;

        }
    }

    @Override
    public void getBeginTime(String year, String month, String day, String hour, String minite) {
        textStringStart = year + "-" + month + "-" + day+" "+hour+":"+minite ;
        startTimeTv.setText(textStringStart);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date parse1=null;
        try {
            parse1 = format.parse(textStringStart);

        } catch (ParseException e) {
            e.printStackTrace();
        }

       if (parse1!=null){
           Log.d("===time==",textStringStart+"=="+parse1.getTime());
           startDate=parse1.getTime()/1000+"";
       }

    }

    @Override
    public void getEndTime(String year, String month, String day, String hour, String minite) {
        textStringEnd = year + "-" + month + "-" + day+" "+hour+":"+minite;
        endTimeTv.setText(textStringEnd);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date parse1=null;
        try {
            parse1 = format.parse(textStringEnd);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (parse1!=null){
            Log.d("===time==",textStringEnd+"=="+parse1.getTime());
            endDate=parse1.getTime()/1000+"";
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity= (ResultEntity) data;

        if (entity!=null){

            if (entity.getEventType()==UserPresenter.BY_ScienerV3_addIcCard_success){

            }else if (entity.getEventType()==UserPresenter.BY_ScienerV3_addIcCard_fail){


            }
        }
    }
}
